package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "品牌的管理")
@RestController
@RequestMapping("admin/product/baseTrademark")
public class BaseTrademarkController {
    @Autowired
    private BaseTrademarkService baseTrademarkService;
    //GET/admin/product/baseTrademark/{page}/{limit}
    //品牌分页列表
    @ApiOperation("品牌分页列表")
    @GetMapping("{page}/{limit}")
    public Result getTradeMarkPage(@PathVariable Long page,
                                   @PathVariable Long limit){
        Page<BaseTrademark> baseTrademarkPage = new Page<>(page,limit);
        //Page iPage1 = new Page(page,limit);

        IPage iPage = baseTrademarkService.getTradeMarkPage(baseTrademarkPage);
        //iPage.getRecords();  List<T> getRecords();
        return Result.ok(iPage);
    }

    // POST/admin/product/baseTrademark/save
    @ApiOperation("品牌保存")
    @PostMapping("save")
    public Result save(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }

    //  DELETE/admin/product/baseTrademark/remove/{id}
    @ApiOperation("品牌删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){

//        baseTrademarkService.remove(new QueryWrapper<BaseTrademark>().eq("id",id));
        baseTrademarkService.removeById(id);
        return Result.ok();
    }

//    根据品牌Id 获取品牌对象
//    GET/admin/product/baseTrademark/get/{id}
    @ApiOperation("根据ID查看品牌信息")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        BaseTrademark baseTrademark = baseTrademarkService.getById(id);
        return Result.ok(baseTrademark);
    }

    // PUT/admin/product/baseTrademark/update
    @ApiOperation("品牌属性修改")
    @PutMapping("update")
    public Result update(@RequestBody BaseTrademark baseTrademark){

//        baseTrademarkService.update(baseTrademark,new QueryWrapper<BaseTrademark>().eq("id",baseTrademark.getId()));
        baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }
}
